package com.ysyx.ysyx_census.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysyx.ysyx_census.dto.BaseDto;
import com.ysyx.ysyx_census.mapper.PassengerreviewPojoMapper;
import com.ysyx.ysyx_census.pojo.OrderPojo;
import com.ysyx.ysyx_census.qo.*;
import com.ysyx.ysyx_census.service.OrderPojoService;
import com.ysyx.ysyx_census.mapper.OrderPojoMapper;
import com.ysyx.ysyx_census.utils.MonthUtils;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class OrderPojoServiceImpl extends ServiceImpl<OrderPojoMapper, OrderPojo>
    implements OrderPojoService{

    @Autowired
    private OrderPojoMapper orderPojoMapper;

    @Autowired
    private PassengerreviewPojoMapper passengerreviewPojoMapper;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public BaseDto driverWages(Integer driverId) {

        Double dailyWages = orderPojoMapper.driverDailyWages(driverId);
        Double monthlyWages = orderPojoMapper.driverMonthlyWages(driverId);
        Integer dailyOrders = orderPojoMapper.driverDailyOrders(driverId);

        if(dailyWages==null){
            dailyWages=0.00;
        }
        if(monthlyWages==null){
            monthlyWages=0.00;
        }
        dailyWages = 0.7*dailyWages;
        monthlyWages = 0.7*monthlyWages;

        DecimalFormat df = new DecimalFormat( "0.00"); //设置double类型小数点后位数格式
        dailyWages = Double.valueOf(df.format(dailyWages));
        monthlyWages = Double.valueOf(df.format(monthlyWages));

        DriverWagesQo driverWagesQo = new DriverWagesQo();
        driverWagesQo.setDailyWage(dailyWages);
        driverWagesQo.setMonthlyWage(monthlyWages);
        driverWagesQo.setDailyOrderNum(dailyOrders);
        List<DriverWagesQo> driverWagesQoList = new ArrayList<>();
        driverWagesQoList.add(driverWagesQo);

        return new BaseDto(0,null,driverWagesQoList);
    }

    @Override
    public List<MonthIncomeQo> sixMonthIncomingCalc() {
        List<String> sixMonth = MonthUtils.getSixMonth();
        List<MonthIncomeQo> incomeList = new ArrayList<>();
        for(int i = 0 ; i < sixMonth.size(); i ++){
            Double income = orderPojoMapper.monthCost(sixMonth.get(i));

            if(income==null){
                income=0.00;
            }else{
                income=income*0.3;
                DecimalFormat df = new DecimalFormat( "0.00"); //设置double类型小数点后位数格式
                income = Double.valueOf(df.format(income));
            }


            MonthIncomeQo tempIncomeQo = new MonthIncomeQo();
            tempIncomeQo.setMonthName(sixMonth.get(i).substring(0,7));
            tempIncomeQo.setMonthIncome(income);

            incomeList.add(tempIncomeQo);
        }

        return incomeList;
    }

    @Override
    public List<MonthOrderNumQo> sixMonthOrderCalc() {
        List<String> sixMonth = MonthUtils.getSixMonth();
        List<MonthOrderNumQo> incomeList = new ArrayList<>();
        for(int i = 0 ; i < sixMonth.size(); i ++){
            int orderNum = orderPojoMapper.monthOrderNum(sixMonth.get(i));
            MonthOrderNumQo monthOrderNumQo = new MonthOrderNumQo();
            monthOrderNumQo.setMonthName(sixMonth.get(i).substring(0,7));
            monthOrderNumQo.setMonthOrderNum(orderNum);
            incomeList.add(monthOrderNumQo);
        }
        return incomeList;
    }

    @Override
    public Integer workingDriver() {

        RSet<Object> set = redissonClient.getSet("working:driver");
        int size = set.size();
        return size;
    }

    @Override
    public Integer monthRegisterPassengerNum() {
        return orderPojoMapper.monthRegisterPassengerNum();
    }

    @Override
    public List<MonthReviewRateQo> monthPassengerReviewRate() {
        List<MonthReviewRateQo> reivewRateList = new ArrayList<>();

        for(int i=1 ; i<6 ; i ++){
            MonthReviewRateQo tempReviewRateQo = new MonthReviewRateQo();
            Integer tempReviewNum = passengerreviewPojoMapper.monthPassengerReviewRateNum(i);
            tempReviewRateQo.setReviewScore(i);
            tempReviewRateQo.setReviewOrderNum(tempReviewNum);
            reivewRateList.add(tempReviewRateQo);
        }

        return reivewRateList;
    }

    @Override
    public List<OrderLngLatQo> weekOrderLngLat() {
        return orderPojoMapper.weekOrderLngLat();
    }

    @Override
    public BaseDto workDriver() {

        Iterable<String> strings = redissonClient.getKeys().getKeysByPattern("work:driver");
        return null;
    }




}




